var vue = new Vue({
   el: "#app",
   data: {
       // 错误提示
       errmsg: "",
       title: "欢迎登录",
       user: {
           nickname: "czr",
           password: "123123",
           code: ""
       }
   },
    mounted() {
       /*document.onkeydown = (e) => {
           if (e.keyCode == 13) {
               this.toLogin();
           }
       }*/
    },
    // 监听
    watch: {
       errmsg: {
           handler(newval,oldvar) {
               // 防止timeout时间叠加
               if (this.stimer) clearTimeout(this.stimer)
               this.stimer = setTimeout(() => {
                   this.errmsg = ""
               }, 2000)
           },
           // 加载就出发 监听
           // immediate: true
       }
    },
    methods: {
       toLogin: throttle(function () {
           let user = this.user;
           console.log(JSON.stringify(user));

           if(!user.nickname){
               this.errmsg = "请输入账号"
               document.getElementById("nickname").focus();
               return;
           }

           if(!user.password){
               this.errmsg = "请输入密码"
               document.getElementById("pwd").focus();
               return;
           }

           if(!user.code){
               this.errmsg = "请输入验证码"
               document.getElementById("code").focus();
               return;
           }

           axios.post("/logined",user).then((res) => {
               if (res.data === "success") {
                   window.location.href = "/";
               } else if (res.data === "failcode") {
                   this.errmsg = "验证码错误"
                   document.getElementById("code").focus();
                   document.getElementById("code").value = "";
                   document.getElementById("ksdimgcode").src = "/kaptcha?d="+new Date().getTime();
               } else {
                   this.errmsg = "输入账号密码有误"
                   document.getElementById("pwd").focus();
                   document.getElementById("pwd").value = "";
               }
           });
       }, 5000)
    }

});